package com.loong.musicdemo.util.dataDeal;

import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.model.Comment;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.util.FormatTimeUtil;
import com.loong.musicdemo.util.HostUtil;
import com.loong.musicdemo.util.httpUtil.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MvDeal {

    //获取歌曲列表中有MV的歌曲mvID
    public static List<String> getMvSongsIdFromSongList(List<Song> songList) {
        List<String> mvSongIds = new ArrayList<>();
        for (Song song : songList) {
            if (!song.getMvId().equals("0")) {
                mvSongIds.add(song.getMvId());
            }
        }
        return mvSongIds;
    }

    //获取mv详细数据
    public static MV getMVDetailByMVId(String mvId) {
        String url = "http://47.112.224.146:3000/mv/detail?mvid=" + mvId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        MV mv = new MV();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject data = object.getJSONObject("data");
            JSONObject brs = data.getJSONObject("brs");
            mv.setMvID(data.getString("id"));
            mv.setMvName(data.getString("name"));
            mv.setArtistName(data.getString("artistName"));
            if (data.getString("cover").startsWith("http://")) {
                mv.setCoverImg(data.getString("cover").replace("http://", "https://"));
            } else {
                mv.setCoverImg(data.getString("cover"));
            }
            mv.setPlayCount(data.getString("playCount"));
            mv.setDuration(data.getString("duration"));
            if (brs.getString("240").startsWith("http://")) {
                mv.setMvUrl(brs.getString("240").replace("http://", "https://"));
            } else {
                mv.setMvUrl(brs.getString("240"));
            }
            mv.save();
            return mv;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getMvUrlByMvId(String mvId){
        String url = "http://47.112.224.146:3000/mv/url?id=" + mvId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        if(response!=null){
            try {
                JSONObject object = new JSONObject(response);
                JSONObject data = object.getJSONObject("data");
                return data.getString("url");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "暂无资源";
    }

    //获取hot评论中的前10条评论
    public static List<Comment> getMvHotCommentsByMvId(String mvId) {
        String url = "http://47.112.224.146:3000/comment/mv?id=" + mvId;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<Comment> comments = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray hotComments = object.getJSONArray("hotComments");
            if (hotComments.length() < 10) {
                for (int i = 0; i < hotComments.length(); i++) {
                    JSONObject hotComment = hotComments.getJSONObject(i);
                    JSONObject user = hotComment.getJSONObject("user");
                    Comment comment = new Comment();
                    comment.setCommentId(hotComment.getString("commentId"));
                    comment.setUserNickName(user.getString("nickname"));
                    comment.setUserAvatarUrl(user.getString("avatarUrl"));
                    comment.setLikeCount(hotComment.getString("likedCount"));
                    comment.setContent(hotComment.getString("content"));
                    comments.add(comment);
                }
            } else {
                for (int i = 0; i < 10; i++) {
                    JSONObject hotComment = hotComments.getJSONObject(i);
                    JSONObject user = hotComment.getJSONObject("user");
                    Comment comment = new Comment();
                    comment.setCommentId(hotComment.getString("commentId"));
                    comment.setUserNickName(user.getString("nickname"));
                    comment.setUserAvatarUrl(user.getString("avatarUrl"));
                    comment.setLikeCount(hotComment.getString("likedCount"));
                    comment.setContent(hotComment.getString("content"));
                    comments.add(comment);
                }
            }
            return comments;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据偏移量以及地区获取MV
     *
     * @param area  地区
     * @param page  当前页数
     * @param limit 获取数目
     * @return mv
     */
    public static List<MV> getMVByOffsetAndArea(String area, int page, int limit) {
        String url = HostUtil.getHost() + "/mv/all?area=" + area + "&offset=" + page * limit + "&limit=" + limit;
        String request = HttpUtil.sendReturnOKHttpRequest(url);
        try {
            if (request != null) {
                JSONObject object = new JSONObject(request);
                JSONArray data = object.optJSONArray("data");
                if (data != null) {//当存在数据时,获取MV信息
                    List<MV> mvs = new ArrayList<>();
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject mvInfo = data.optJSONObject(i);
                        MV mv = getMvByJSONObject(mvInfo);
                        mvs.add(mv);
                    }
                    return mvs;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据解析到的JSONObject对象获取MV
     *
     * @param mvInfo jsonobject对象
     * @return mv
     */
    public static MV getMvByJSONObject(JSONObject mvInfo) {
        MV mv = new MV();
        mv.setArtistName(mvInfo.optString("artistName"));
        mv.setMvID(mvInfo.optString("id"));
        String coverImg = mvInfo.optString("picUrl") + mvInfo.optString("cover");
        mv.setCoverImg(coverImg.startsWith("http://") ? coverImg.replace("http://", "https://") : coverImg);//获取https开头网址
        mv.setDuration(mvInfo.optString("duration"));
        mv.setMvName(mvInfo.optString("name"));
        String playCount = mvInfo.optString("playCount");
        mv.setPlayCount(playCount);//格式化播放数
        return mv;
    }

    /**
     * 根据完整信息获取MV
     *
     * @param area  地区
     * @param type  类型
     * @param sort  排序方式
     * @param limit 限制
     * @param page  分页数
     * @return MV集合
     */
    public static List<MV> getMvByAllInfo(String area, String type, String sort, int limit, int page) {
        String url = HostUtil.getHost() + "/mv/all?area=" + area + "&type=" + type + "&order=" + sort + "&offset=" + page * limit + "&limit=" + limit;
        String request = HttpUtil.sendReturnOKHttpRequest(url);//请求数据
        try {
            if (request != null) {
                JSONObject object = new JSONObject(request);
                JSONArray data = object.optJSONArray("data");
                if (data != null) {//当存在数据时,获取MV信息
                    List<MV> mvs = new ArrayList<>();
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject mvInfo = data.optJSONObject(i);
                        MV mv = getMvByJSONObject(mvInfo);
                        mvs.add(mv);
                    }
                    return mvs;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 根据请求路径获取ＭＶ
     */
    public static List<MV> getMVByUrl(String url) {
        String request = HttpUtil.sendReturnOKHttpRequest(HostUtil.getHost() + url);//根据请求路径获取
        List<MV> dailyRecommendMV = new ArrayList<>();//日推MV
        try {
            if (request != null) {
                JSONObject object = new JSONObject(request);
                JSONArray result = object.optJSONArray("result");
                if (result != null) {
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject mvInfo = result.optJSONObject(i);
                        MV mv = MvDeal.getMvByJSONObject(mvInfo);
                        dailyRecommendMV.add(mv);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return dailyRecommendMV;
    }
}
